
#include <stdio.h>
#include "esp_log.h"
#include "driver/i2c.h"
#include "sdkconfig.h"
#include "ina226.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

static const char *TAG = "ina226";

#define I2C_MASTER_FREQ_HZ CONFIG_I2C_MASTER_FREQUENCY        /*!< I2C master clock frequency */
#define I2C_MASTER_TX_BUF_DISABLE 0                           /*!< I2C master doesn't need buffer */
#define I2C_MASTER_RX_BUF_DISABLE 0 

#define ACK_CHECK_EN 0x1                        /*!< I2C master will check ack from slave*/
#define ACK_CHECK_DIS 0x0                       /*!< I2C master will not check ack from slave */
#define ACK_VAL 0x0                             /*!< I2C ack value */
#define NACK_VAL 0x1                            /*!< I2C nack value */

void Ina226::INA226_Read2Byte(uint8_t reg_addr, uint16_t *reg_data)
{
    uint8_t buf[2];
    uint8_t ret;
    i2c_cmd_handle_t cmd = i2c_cmd_link_create();
    i2c_master_start(cmd);
    i2c_master_write_byte(cmd, WRITE_ADDR, ACK_CHECK_EN);
    i2c_master_write_byte(cmd, reg_addr, ACK_CHECK_EN);
    i2c_master_start(cmd);
    i2c_master_write_byte(cmd, READ_ADDR, ACK_CHECK_EN);
    i2c_master_read_byte(cmd, buf, (i2c_ack_type_t)ACK_VAL);
    i2c_master_read_byte(cmd, (buf+1), (i2c_ack_type_t)ACK_VAL);
    i2c_master_stop(cmd);
    ret = i2c_master_cmd_begin(ina_port, cmd, 1000 / portTICK_PERIOD_MS);
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "INA226_Read2Byte_error");
    }
    i2c_cmd_link_delete(cmd);
    *reg_data = (uint16_t)((buf[0] << 8) | buf[1]);
}

void Ina226::INA226_Write2Byte(uint8_t reg_addr,uint16_t reg_data)
{
    uint8_t data_high=(uint8_t)((reg_data&0xFF00)>>8);
	uint8_t data_low=(uint8_t)reg_data&0x00FF;

    uint8_t ret;
    i2c_cmd_handle_t cmd = i2c_cmd_link_create();
    i2c_master_start(cmd);
    i2c_master_write_byte(cmd, WRITE_ADDR, ACK_CHECK_EN);
    i2c_master_write_byte(cmd, reg_addr, ACK_CHECK_EN);
    i2c_master_write_byte(cmd, data_high, ACK_CHECK_EN);
    i2c_master_write_byte(cmd, data_low, ACK_CHECK_EN);
    i2c_master_stop(cmd);
    ret = i2c_master_cmd_begin(ina_port, cmd, 1000 / portTICK_PERIOD_MS);
    i2c_cmd_link_delete(cmd);
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "INA226_Write2Byte_error");
    }
    vTaskDelay(30 / portTICK_PERIOD_MS);
}

Ina226::Ina226(i2c_port_t i2c_master_port, int sda_io, int scl_io)
{
    ina_port = i2c_master_port;
    sda_io_num = sda_io;
    scl_io_num = scl_io;
}

void Ina226::INA226_Init(void)
{
	//初始化接口
    i2c_config_t conf = {
        .mode = I2C_MODE_MASTER,
        .sda_io_num = sda_io_num,
        .scl_io_num = scl_io_num,
        .sda_pullup_en = GPIO_PULLUP_ENABLE,
        .scl_pullup_en = GPIO_PULLUP_ENABLE,
        .master = {
            .clk_speed = I2C_MASTER_FREQ_HZ,
        },
        .clk_flags = 0,          /*!< Optional, you can use I2C_SCLK_SRC_FLAG_* flags to choose i2c source clock here. */
    };
    esp_err_t err = i2c_param_config(ina_port, &conf);
    if (err != ESP_OK) {
        ESP_LOGE(TAG, "i2c_param_config_error");
    }
    err = i2c_driver_install(ina_port, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
    if (err != ESP_OK) {
        ESP_LOGE(TAG, "i2c_driver_install_error");
    }
    ESP_LOGI(TAG, "Initializing i2c_master");
	vTaskDelay(10/portTICK_PERIOD_MS);
	
    uint16_t buf;
    //重新启动
    INA226_Write2Byte(Config_Reg, 0x8000);
	ESP_LOGI(TAG, "Config_Reg 0x8000");
	//写配置寄存器
	INA226_Write2Byte(Config_Reg, 0x4527);//0100_010_100_100_111 //16次平均,1.1ms,1.1ms,连续测量分流电压和总线电压
	ESP_LOGI(TAG, "Config_Reg 0x4527");
	//写校准寄存器
	//LSB选择0.02mA,分压电阻选0.0001R Cal=0.00512/(0.02mA*0.0001R)=2560
	INA226_Write2Byte(Calib_Reg, 0x0400);
    ESP_LOGI(TAG, "Calib_Reg 0x0400");
    // get id
    INA226_Read2Byte(ID_Reg, &buf);
    ESP_LOGI(TAG, "ID_Reg %d", buf);
}

float Ina226::INA226_GetVoltage(void)
{
    uint16_t buf;

    INA226_Read2Byte(Bus_V_Reg, &buf);
    Voltage = buf*1.25f*1e-3;
    printf("data=%-5d, Voltage =%f V\r\n", buf,Voltage);

    return Voltage;
}

//获取分流电流mA
float Ina226::INA226_GetShunt_Current(void)
{
   uint16_t buf;

   INA226_Read2Byte(Shunt_V_Reg, &buf);
   if ((buf>>15)==1){
        ESP_LOGI(TAG, "(buf>>15)==1");
        buf=~buf+1;
        Curent=-buf*2.5e-3*rnf*1e-3;
   }
   else{
        Curent=buf*2.5e-3*rnf*1e-3;
   }
   printf("data=%-5d, Curent =%f A\r\n", buf, Curent);
   return Curent;
}

